//
//  UITableView+Util.m
//  YDGJ
//
//  Created by ydcq on 16/6/22.
//  Copyright © 2016年 Galaxy360. All rights reserved.
//

#import "UITableView+Util.h"
#import "UILabel+Util.h"

static char const * const kEmptyStrKey    = "kEmptyStrKey";
static char const * const kNetErrorStrKey = "kNetErrorStrKey";
static char const * const kButtonKey      = "kButtonKey";
static char const * const kBlockKey       = "kBlockKey";

@interface UITableView ()

@end

@implementation UITableView (Util)

+ (instancetype)plainTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, KSystemNavBarHeight+20, Screen_Width, Screen_Height-KSystemNavBarHeight-20) style:UITableViewStylePlain];
    tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    tableView.rowHeight = kDefaultCellHeight;
    tableView.backgroundColor = Color_BackGround;
    tableView.separatorColor = RGB(229,229,229);
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 0.01)];
    headView.backgroundColor = tableView.backgroundColor;
    tableView.tableHeaderView = headView;
    tableView.tableFooterView = [UIView new];
    return tableView;
}

+ (instancetype)groupedTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, KSystemNavBarHeight+20, Screen_Width, Screen_Height-KSystemNavBarHeight-20) style:UITableViewStyleGrouped];
    tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, kTableViewTopSpace)];
    tableView.rowHeight = kDefaultCellHeight;
    tableView.backgroundColor = Color_BackGround;
    tableView.separatorColor = RGB(229,229,229);
    tableView.sectionHeaderHeight = 0.01;
    tableView.sectionFooterHeight = kTableViewTopSpace;
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 0.01)];
    return tableView;
}

+ (UIView *)headerViewWithText:(NSString *)text
{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, SECTION_HEADER_H)];
    backView.backgroundColor = Color_BackGround;
    
    UILabel *label = [UILabel labelWithFrame:CGRectMake(15, 0, Screen_Width-15*2, SECTION_HEADER_H) text:text fontSize:14 textColor:UIColorFromRGB(0x666666) textAlignment:NSTextAlignmentLeft];
    [backView addSubview:label];
    
    return backView;
}

#pragma mark 列表状态相关

- (void)setEmptyString:(NSString *)emptyString
{
    objc_setAssociatedObject(self, kEmptyStrKey, emptyString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)emptyString
{
    return objc_getAssociatedObject(self, kEmptyStrKey);
}

- (void)setNetErrorString:(NSString *)netErrorString
{
    objc_setAssociatedObject(self, kNetErrorStrKey, netErrorString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)netErrorString
{
    return objc_getAssociatedObject(self, kNetErrorStrKey);
}

- (void)createButtonForState:(NSInteger)state clickBlock:(void(^)())blk
{
    id vc = [self viewcontroller];
    if ([vc isKindOfClass:NSClassFromString(@"GJCouponVC")]) {
        return;
    }
    UIButton *btn = objc_getAssociatedObject(self, kButtonKey);
    if (!btn) {
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.center = CGPointMake(self.width/2.f, self.height/2.f);
        btn.bounds = CGRectMake(0, 0, Screen_Width-21*2, 30);
        objc_setAssociatedObject(self, kButtonKey, btn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self addSubview:btn];
    }
    btn.hidden = NO;
    [self bringSubviewToFront:btn];
    btn.userInteractionEnabled = YES;
    
    if (state==0) {
        NSMutableAttributedString *mStr = [[NSMutableAttributedString alloc] initWithString:self.emptyString?:@"暂无数据" attributes:@{NSFontAttributeName:FontCustomSize(14),NSForegroundColorAttributeName:UIColorFromRGB(0xa4a4a4)}];
        [btn setAttributedTitle:mStr forState:UIControlStateNormal];
        btn.userInteractionEnabled = NO;
    }
    else
    {
        NSMutableAttributedString *mStr = [[NSMutableAttributedString alloc] initWithString:self.netErrorString?:@"加载失败，重新刷新" attributes:@{NSFontAttributeName:FontCustomSize(16),NSForegroundColorAttributeName:UIColorFromRGB(0xa4a4a4)}];
        
        NSInteger index = [mStr.string rangeOfString:@"，"].location;
        
        if (index==NSNotFound) {
            [btn setAttributedTitle:mStr forState:UIControlStateNormal];
            return;
        }
        [mStr setAttributes:@{NSFontAttributeName:FontCustomSize(16),NSForegroundColorAttributeName:Color_Main} range:NSMakeRange(index+1, mStr.string.length-index-1)];
        [btn setAttributedTitle:mStr forState:UIControlStateNormal];
        
        NSMutableAttributedString *mStr1 = mStr.mutableCopy;
        [mStr1 setAttributes:@{NSFontAttributeName:FontCustomSize(16),NSForegroundColorAttributeName:[Color_Main colorWithAlphaComponent:0.4]} range:NSMakeRange(index+1, mStr1.string.length-index-1)];
        [btn setAttributedTitle:mStr1 forState:UIControlStateHighlighted];
        
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        objc_setAssociatedObject(self, kBlockKey, blk, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    return;
}

- (void)createButtonForState:(NSInteger)state withImage:(UIImage *)img title:(NSString *)title clickBlock:(void(^)())blk
{
    id vc = [self viewcontroller];
    if ([vc isKindOfClass:NSClassFromString(@"GJCouponListVC")]) {
        return;
    }
    if (!img && ![vc isKindOfClass:NSClassFromString(@"GJMyPayRecordsVC")]) {
        return [self createButtonForState:state clickBlock:blk];
    }
    UIButton *btn = objc_getAssociatedObject(self, kButtonKey);
    if (!btn) {
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.center = CGPointMake(self.width/2.f, self.height/2.f);
        btn.bounds = CGRectMake(0, 0, self.width-15*2, self.height-15*2);
        objc_setAssociatedObject(self, kButtonKey, btn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self addSubview:btn];
    }
    btn.hidden = NO;
    [self bringSubviewToFront:btn];
    btn.userInteractionEnabled = state==1;
    
    //判断类型
    if ([vc isKindOfClass:NSClassFromString(@"GJMyPayRecordsVC")]) {
        img = [UIImage imageNamed:@"icon_zanwujilu1"];
        title = @"暂无相关记录";
    }
    
    CGFloat width = img.size.width;
    CGFloat height = img.size.height;
    CGFloat totalH = height+24+12;
    btn.frame = CGRectMake(15, self.height/2.f-totalH/2.f, self.width-15*2, totalH);
    
    [btn.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIImageView *flgImgView = [[UIImageView alloc] initWithImage:img];
    flgImgView.frame = CGRectMake(btn.width/2.f-width/2.f, 0, width, height);
    [btn addSubview:flgImgView];
    
    UILabel *label = [UILabel labelWithFrame:CGRectMake(0, height+24, btn.width, 12) text:title fontSize:12 textColor:UIColorFromRGB(0xa4a4a4) textAlignment:NSTextAlignmentCenter];
    [btn addSubview:label];
}

- (void)btnClicked:(id)sender
{
    void (^blk)() = objc_getAssociatedObject(self, kBlockKey);
    if (blk) {
        [self hideButton];
        blk();
    }
}

- (void)hideButton
{
    [objc_getAssociatedObject(self, kButtonKey) setHidden:YES];
}
-(UIButton *)getStatusBtn
{
    return objc_getAssociatedObject(self, kButtonKey);
}
@end
